package com.jt.www.common.controller;

import com.jt.www.common.service.JunZiQianService;
import com.jt.www.common.service.zqsign.param.agreement.Param;
import com.jt.www.exception.ServiceException;
import com.jt.www.model.reps.GenericDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @描述： 君子签
 * @作者： lq
 * @创建日期： 2020-9-28 14:11
 * @版权： 江泰保险经纪股份有限公司
 */
@Api(value = "君子签", tags = {"Junziqian-API"}, description = "君子签")
@RestController
@RequestMapping("/jzq/api")
public class JunziqianController {

    public static final Logger log = LoggerFactory.getLogger(JunziqianController.class);

    @Autowired
    private JunZiQianService junZiQianService;

    /**
     * @param agreementParam
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "新建委托协议", notes = "新建委托协议", httpMethod = "POST")
    @PostMapping(value = "/agreement", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> agreement(@RequestBody @ApiParam(name = "新建委托协议参数", value = "传入json格式", required = true)
                                       @Valid Param agreementParam,
                                       HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        try {
            return junZiQianService.saveAgreement(agreementParam, request, response);
        } catch (Exception e) {
            log.error("创建协议接口/agreement", e.getMessage());
            return GenericDataResponse.ng(e.getMessage());
        }
    }

    /**
     * 签章信息回调
     *
     * @return
     */
    @PostMapping(value = "/_bai/affirmBack", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiOperation(value = "签章信息回调")
    public String affirmBack(@RequestParam("method") String method, @RequestParam("timestamp") Long timestamp,
                             @RequestParam("sign") String sign, @RequestParam("appkey") String appkey,
                             @RequestParam("data") String data) {
        return junZiQianService.affirmBack(method, timestamp, sign, appkey, data);
    }

}
